<?php 

namespace App\ModelFilters;

use EloquentFilter\ModelFilter;

class CateFilter extends ModelFilter
{
    //$drop_id = true时，查询 a=?和a_id=?一样返回a方法
    protected $drop_id = false;

    /**
     * 黑名单中方法不可执行
     * 可动态打开例
     *  if(Auth::user()->isAdmin()) {
     *       $this->whitelistMethod('blacktest');
     *  }     
     * @var array
     * @Author 老蟹 7040867@qq.com    DateTime 2020-01-08
     */
    protected $blacklist = ['blacktest'];
    
    /**
    * Related Models that have ModelFilters as well as the method on the ModelFilter
    * As [relationMethod => [input_key1, input_key2]].
    *
    * @var array
    */    
    public $relations = ['posts','posts.user'];

    public function setup()
    {
        return $this->with('posts.user');
    }
    public function group($group)
    {
        return $this->related('posts.user', 'created_at', '>', $group);
        
        // This would also be shorthand for the same query
        // return $this->related('clients', 'industry_id', $id);
    }

    public function postsSetup($query)
    {
        return $query->position('a');
    }

    //这是一个黑名单中方法测试
    public function blacktest($name)
    {
        return $this->where('groups', 'LIKE', "%$name%");
    }
    public function name($name)
    {
        return $this->where(function($q) use ($name)
                            {
                                return $q->where('name', 'LIKE', "%$name%")
                                    ->orWhere('title', 'LIKE', "%$name%");
                            }
                        );
    }
    public function groups($name)
    {
        return $this->where('groups', 'LIKE', "%$name%");
    }
}
